package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.*;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 应聘控制器
 */
@Controller
@RequestMapping("/jobApply")
public class JobApplyController extends BaseService {
    @Autowired
    private JobApplyService jobApplyService;
    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private RecruitInfoService recruitInfoService;
    @Autowired
    private PositionTypeService positionTypeService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private CvService cvService;


    @RequestMapping(value = "/editById", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/6
     * function: 根据id修改应聘表记录
     *
     * @param id
     * @return
     */
    public ResponseBean editById(@RequestBody JobApply jobApply) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (checkDeleteUpdateIsSuccess(jobApplyService.modifyOneById(jobApply))) {
                responseBean.setSuccess(true);
                responseBean.setMessage("修改成功");
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("修改失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/6
     * function: 投递简历
     *
     * @param jobApply
     * @return
     */
    public ResponseBean addOne(@RequestBody JobApply jobApply) {
        ResponseBean responseBean = new ResponseBean();
        try {
            jobApplyService.addOne(jobApply);
            responseBean.setSuccess(true);
            responseBean.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/9
     * function: 学生应聘模糊筛选
     *
     * @param jobApply
     * @return
     */
    public ResponseBean findByCondition(@RequestBody JobApply jobApply) {
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (jobApply.getPageIndex() > 0) jobApply.setPageIndex(jobApply.getPageIndex() - 1);
            List<JobApply> jobApplyList = jobApplyService.findByCondition(jobApply);
            for (JobApply jobApply1 : jobApplyList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("jobApply", jobApply1);
                //查找用人单位名称信息
                if (jobApply1.getEmployerid() != null) {
                    OrgInfo orgInfo = orgInfoService.findByid(jobApply1.getEmployerid());
                    jsonObject.put("orgInfoFullName", orgInfo.getFullname());
                }
                //查找对应岗位
                if (jobApply1.getRecruitinfoid() != null){
                    RecruitInfo recruitInfo = recruitInfoService.findRecruitInfoById(jobApply1.getRecruitinfoid());
                    if (recruitInfo != null){
                        PositionType positionType = positionTypeService.findById(recruitInfo.getPositiontype());
                        jsonObject.put("positionTypeName", positionType.getTypename());
                    }
                }
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(jobApply.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findByCondition2", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/9
     * function: 企业应聘模糊筛选
     *
     * @param jobApply
     * @return
     */
    public ResponseBean findByCondition2(@RequestBody JobApply jobApply) {
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (jobApply.getPageIndex() > 0) jobApply.setPageIndex(jobApply.getPageIndex() - 1);
            List<JobApply> jobApplyList = jobApplyService.findByCondition(jobApply);
            for (JobApply jobApply1 : jobApplyList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("jobApply", jobApply1);
                //获取学生详细信息
                if (jobApply1.getStid() != null){
                    StudentInfoDetail studentInfoDetail = studentInfoService.findDetailByUserId(jobApply1.getStid());
                    jsonObject.put("studentInfoDetail", studentInfoDetail);
                }
                //获取简历信息
                if (jobApply1.getCvid() != null){
                    Cv cv = cvService.findOneById(jobApply1.getCvid());
                    jsonObject.put("cv", cv);
                }
                //获取对应招聘信息
                if (jobApply1.getRecruitinfoid() != null){
                    RecruitInfo recruitInfo = recruitInfoService.findRecruitInfoById(jobApply1.getRecruitinfoid());
                    jsonObject.put("recruitInfo", recruitInfo);
                    if (recruitInfo != null){
                        jsonObject.put("positiontype", positionTypeService.findById(recruitInfo.getPositiontype()));
                    }
                }
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(jobApply.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }
    
    @RequestMapping(value = "/findOneById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/10
     * function: 根据id查找单条应聘记录
     *
     * @param id
     * @return
     */
    public ResponseBean findOneById(@PathVariable("id") Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(jobApplyService.findOneById(id));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

}
